package com.chif.mapreduce.WritableComparable;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FlowDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1 获取Job对象
        Configuration conf = new Configuration();
        //指定hadoop运行时在本地的临时工作目录
        conf.set("hadoop.tmp.dir", "D:/tmp/mapreduce_tmp_cache");

        Job job = Job.getInstance(conf);

        //todo MapTask运行设置

        //如果不设置InputFormat，它默认用的是TextInputFormat.class
        //job.setInputFormatClass(CombineTextInputFormat.class);

        //虚拟存储切片的最大值设置20M
        //CombineTextInputFormat.setMaxInputSplitSize(job,20971520);

        //todo Shuffle运行设置

        //设置自定义Partition分区器
        job.setPartitionerClass(ProvincePartitioner.class);

        //todo ReduceTask运行设置

        //自定义Partition后，要根据自定义Partitioner的逻辑设置相应数量的ReduceTask
        job.setNumReduceTasks(2);

        //2 关联本Driver类 Mapper、Reducer
        job.setJarByClass(FlowBean.class);
        job.setMapperClass(FlowMapper.class);
        job.setReducerClass(FlowReducer.class);

        //3 设置Map端输出KV类型
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);

        //4 设置程序最终输出的KV类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //6 设置程序的输入输出路径
        FileInputFormat.setInputPaths(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\input2"));
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\output"));

        //7 提交Job
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}
